package com.lzdongrui.framework.web.service;

import com.lzdongrui.common.core.domain.entity.ClientRole;
import com.lzdongrui.common.core.domain.entity.ClientUser;
import com.lzdongrui.common.core.domain.entity.SysRole;
import com.lzdongrui.common.core.domain.entity.SysUser;
import com.lzdongrui.system.service.IClientUserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class ClientPermissionService {

//    @Autowired
//    private IClientUserRoleService clientUserRoleService;

    @Autowired
    private IClientUserMenuService clientUserMenuService;

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(ClientUser user) {
        Set<String> perms = new HashSet<String>();
            List<ClientRole> roles = user.getClientRoles();
            if (!roles.isEmpty() && roles.size() > 1)
            {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (ClientRole role : roles)
                {
                    Set<String> rolePerms = clientUserMenuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            }
            else
            {
                perms.addAll(clientUserMenuService.selectMenuPermsByUserId(user.getId()));
            }
        return perms;
    }
}
